package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;

import com.sky.utils.JwtUtil;
import com.sky.utils.ParaVerifUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */

    @PostMapping("/login")
    @ApiOperation(value = "登录接口", notes = "登录接口")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
//        Map<String, Object> claims = new HashMap<>();
//
//        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
//        String token = JwtUtil.createJWT(
//                jwtProperties.getAdminSecretKey(),
//                jwtProperties.getAdminTtl(),
//                claims);
//
        //以用户的id作为jwt的签名，设置秘钥，设置时间
        String token= JwtUtil.createToekn(employee.getId(), jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl());

        //使用builder注解来构造创建EmployeeLoginVO对象
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        //从上下文中获取登录用户信息
//        String loginUsername=Authentication.getLoginUsername();

        return Result.success(employeeLoginVO);
    }

    //注册
    @PostMapping("/register")
    @ApiOperation(value = "注册接口", notes = "注册接口")
    public Result<String> register() {

        return Result.success();

    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "退出接口", notes = "退出接口")
    public Result<String> logout() {
        return Result.success();
    }



    //新增员工
    @PostMapping("/AddEmploy")
    @ApiOperation(value = "新增员工", notes = "新增员工")
    public Result<String> AddEmploy(@RequestBody EmployeeDTO employeeDTO) {
//        从上下文获取登录用户信息，.....
        Long LoginID = BaseContext.getCurrentId();
        employeeService.AddEmploy(employeeDTO,LoginID);
        return Result.success();
    }

    //分页查询员工
    @GetMapping("/GetEmployList")
    @ApiOperation(value = "分页查询员工", notes = "分页查询员工")
    public Result<PageResult> GetEmployList(EmployeePageQueryDTO employeePageQueryDTO) {
        //需要从前端传过来用户姓名、页码、每页条数
        PageResult result = employeeService.GetEmployList(employeePageQueryDTO);
        return Result.success(result);
    }

    //员工启用、禁用
    @PostMapping("/EmployStatus/{status}")
    @ApiOperation(value = "员工启用、禁用", notes = "员工启用、禁用")
    public Result<String> EmployStatus(@PathVariable Integer status, Long id) {
        //需要从前端传过来员工id和状态
//        id是从？url中获取
        if (id == null) {
            return Result.error("请正确选择员工");
        }
        if (status == null) {
            return Result.error("请正确切换状态");
        }
        boolean flag = employeeService.EmployStatus(status, id);
        return flag? Result.success() : Result.error("启用、禁用失败");
    }

    //查询员工信息
    @GetMapping("/GetEmployInfo/{id}")
    @ApiOperation(value = "查询员工信息", notes = "查询员工信息")
    public Result<EmployeeDTO> GetEmployInfo(@PathVariable Long id) {
        //需要从前端传过来员工id
        if (ParaVerifUtil.isNull(id)) {
            return Result.error("请正确选择员工");
        }
        //查询
        Employee employee = employeeService.GetEmployInfo(id);

        //将employee对象复制为employeeDTO对象
        EmployeeDTO employeeDTO = new EmployeeDTO();
        BeanUtils.copyProperties(employee, employeeDTO);

        return Result.success(employeeDTO);
    }

    //修改员工信息
    @PostMapping("/UpdateEmploy")
    @ApiOperation(value = "修改员工信息", notes = "修改员工信息")
    public Result<String> UpdateEmploy(@RequestBody EmployeeDTO employeeDTO) {
        //......校验参数.....
        if (ParaVerifUtil.isEmpty(employeeDTO.getId())){
            return Result.error("请正确选择员工");
        }
        Long LoginID = BaseContext.getCurrentId();
        boolean flag = employeeService.UpdateEmploy(employeeDTO,LoginID);
        return Result.success();
    }



}
